﻿Imports System.Windows.Forms
Imports Encryptor.My

Public Class dlgChPass
    Private Enum Validity As Int32
        RequiredIsBlank = 0
        AuthFailure = 1
        ConfirmFailure = 2
        AllIsValid = 3
    End Enum

    Dim valid As Validity

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        CheckValidity(txtNewPass.Text, txtCurPass.Text, txtConfirm.Text)
        Select Case valid
            Case Validity.RequiredIsBlank
                MsgBox("One or more required fields are blank.")
                ClearAll()
                txtCurPass.Focus()
                Exit Sub
            Case Validity.AuthFailure
                MsgBox("Current password incorrect. Aborting change.")
                ClearAll()
                txtCurPass.Focus()
                Exit Sub
            Case Validity.ConfirmFailure
                MsgBox("Confirmation does not match. Aborting change.")
                ClearAll()
                txtCurPass.Focus()
                Exit Sub
            Case Validity.AllIsValid
                MySettings.Default.password = txtNewPass.Text
                MySettings.Default.Save()
                MsgBox("Password successfully changed." & vbCrLf & "NOTE: Any files you previously encrypted are now indecipherable unless you revert your password.")
                ClearAll()
        End Select
        frmCrypto.Show()
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        ClearAll()
        frmCrypto.Show()
        Me.Close()
    End Sub

    Private Sub dlgChPass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If MySettings.Default.password = "" Or IsNothing(MySettings.Default.password) Then
            MySettings.Default.password = "newuser"
            txtCurPass.Text = MySettings.Default.password
            txtNewPass.Focus()
        Else
            ClearAll()
        End If
    End Sub

    Private Function CheckValidity(ByVal newPwd As String, ByVal curPwd As String, ByVal confPwd As String) As Validity
        If IsNothing(newPwd) Or IsNothing(curPwd) Or IsNothing(confPwd) Or newPwd = "" Or curPwd = "" Or confPwd = "" Then
            valid = Validity.RequiredIsBlank
            Return valid
        ElseIf curPwd <> MySettings.Default.password Then
            valid = Validity.AuthFailure
            Return valid
        ElseIf newPwd <> confPwd Then
            valid = Validity.ConfirmFailure
            Return valid
        ElseIf newPwd = confPwd And curPwd = MySettings.Default.password Then
            valid = Validity.AllIsValid
            Return valid
        Else
            Return CType("", Validity)
        End If
    End Function

    Private Sub ClearAll()
        txtNewPass.Text = Nothing
        txtConfirm.Text = Nothing
        txtCurPass.Text = Nothing
    End Sub
End Class